#include "matrixformatconverter.h"

MatrixFormatConverter::MatrixFormatConverter(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	ui.setupUi(this);

	ui.listWidget->addItem("Row Format");
	ui.listWidget->addItem("CSR 0");
	ui.listWidget->addItem("CSR 1");
	ui.listWidget->addItem("CSR 1 lower and upper");

	ui.listWidget->setCurrentRow(0);

	connect(ui.openPushButton,SIGNAL(clicked()),this,SLOT(openFile()));
	connect(ui.savePushButton,SIGNAL(clicked()),this,SLOT(saveFile()));

}

MatrixFormatConverter::~MatrixFormatConverter()
{

}

/**
*********************************SLOTS**********************************
*/
void MatrixFormatConverter::openFile()
{
	QString fileName;
	if(!(fileName=QFileDialog::getOpenFileName(this,"Open Matrix files","","Matrix files ( *.mtx ) ;; All files ( *.* )")).isNull())
	{
		ui.openLineEdit->setText(fileName);
	}
}

void MatrixFormatConverter::saveFile()
{
	if(!ui.openLineEdit->text().isEmpty())
	{
		QString fileName;
		if(!(fileName=QFileDialog::getSaveFileName(this,"Save Matrix ","","Matrix files ( *.mtx ) ;; All files (*.*)")).isNull())
		{
			ui.saveLineEdit->setText(fileName);
			int selectedFormat=ui.listWidget->currentRow();

			switch(selectedFormat)
			{
			case 0:
				{
					saveToRowFormat();
					break;
				}
			case 1:
				{
					saveToCSRFormat();
					break;
				}
			case 2:
				{
					saveToCSR_1_Format();
					break;
				}
			case 3:
				{
					saveToCSR_1_L_U_Format();
					break;
				}
			default:
				{}
			}

		}
		QMessageBox msg;
		msg.setText("Save successful!!!");
		msg.exec();
	} else
	{
		QMessageBox msg;
		msg.setText("Befor convert, you have to open a matrix file !!!");
		msg.exec();
	}


}


/**
	Converter functions
*/

void MatrixFormatConverter::saveToRowFormat()
{
	CSparseMatrixCSR<double> rowFormat(ui.openLineEdit->text().toAscii().data());
	rowFormat.printMToFile(ui.saveLineEdit->text().toAscii().data());
}

void MatrixFormatConverter::saveToCSRFormat()
{
	CSparseMatrixCSR<double> rowFormat(ui.openLineEdit->text().toAscii().data());
	rowFormat.printMToFileCSR_0_Format(ui.saveLineEdit->text().toAscii().data());
}

void MatrixFormatConverter::saveToCSR_1_Format()
{
	CSparseMatrixCSR1<double> rowFormat(ui.openLineEdit->text().toAscii().data());
	rowFormat.printMToFile(ui.saveLineEdit->text().toAscii().data());

}

void MatrixFormatConverter::saveToCSR_1_L_U_Format()
{
	CSparseMatrixCSR1<double> rowFormat(ui.openLineEdit->text().toAscii().data());
	rowFormat.printMToFile_L_U(ui.saveLineEdit->text().toAscii().data());
}



